package com.ruoyi.basedata.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.WfzGybaseunitMapper;
import com.ruoyi.basedata.domain.WfzGybaseunit;
import com.ruoyi.basedata.service.IWfzGybaseunitService;

/**
 * 盖亚组织Service业务层处理
 * 
 * @author xiefeng
 * @date 2022-12-06
 */
@Service
public class WfzGybaseunitServiceImpl implements IWfzGybaseunitService 
{
    @Autowired
    private WfzGybaseunitMapper wfzGybaseunitMapper;

    /**
     * 查询盖亚组织
     * 
     * @param fid 盖亚组织主键
     * @return 盖亚组织
     */
    @Override
    public WfzGybaseunit selectWfzGybaseunitByFid(Long fid)
    {
        return wfzGybaseunitMapper.selectWfzGybaseunitByFid(fid);
    }

    /**
     * 查询盖亚组织列表
     * 
     * @param wfzGybaseunit 盖亚组织
     * @return 盖亚组织
     */
    @Override
    public List<WfzGybaseunit> selectWfzGybaseunitList(WfzGybaseunit wfzGybaseunit)
    {
        return wfzGybaseunitMapper.selectWfzGybaseunitList(wfzGybaseunit);
    }

    /**
     * 新增盖亚组织
     * 
     * @param wfzGybaseunit 盖亚组织
     * @return 结果
     */
    @Override
    public int insertWfzGybaseunit(WfzGybaseunit wfzGybaseunit)
    {
        return wfzGybaseunitMapper.insertWfzGybaseunit(wfzGybaseunit);
    }

    /**
     * 修改盖亚组织
     * 
     * @param wfzGybaseunit 盖亚组织
     * @return 结果
     */
    @Override
    public int updateWfzGybaseunit(WfzGybaseunit wfzGybaseunit)
    {
        return wfzGybaseunitMapper.updateWfzGybaseunit(wfzGybaseunit);
    }

    /**
     * 批量删除盖亚组织
     * 
     * @param fids 需要删除的盖亚组织主键
     * @return 结果
     */
    @Override
    public int deleteWfzGybaseunitByFids(Long[] fids)
    {
        return wfzGybaseunitMapper.deleteWfzGybaseunitByFids(fids);
    }

    /**
     * 删除盖亚组织信息
     * 
     * @param fid 盖亚组织主键
     * @return 结果
     */
    @Override
    public int deleteWfzGybaseunitByFid(Long fid)
    {
        return wfzGybaseunitMapper.deleteWfzGybaseunitByFid(fid);
    }

    @Override
    public String importData(List<WfzGybaseunit> wfzGybaseunitList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(wfzGybaseunitList) || wfzGybaseunitList.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<WfzGybaseunit> existList = selectWfzGybaseunitList(null);
        for (WfzGybaseunit importData : wfzGybaseunitList)
        {
            try {

                boolean userFlag = false;
                for (WfzGybaseunit entry : existList) {
                    if (entry.getFnumber().equals(importData.getFnumber())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertWfzGybaseunit(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getFnumber() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateWfzGybaseunit(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getFnumber() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getFnumber() + " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + importData.getFnumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
